package cn.ywang.mybatis;

import cn.ywang.mybatis.dto.TestTaskDto;
import cn.ywang.mybatis.model.Member;
import cn.ywang.mybatis.model.TestTask;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

/**
 * @author BeatificWang
 * @date 2019/8/17 16:05
 */

public class MybatisDemoOne {

	@Test
	public void testStatement() {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		SqlSession sqlSession = sqlSessionFactory.openSession();
		Member member = sqlSession.selectOne("cn.ywang.mybatis.mapper.MemberMapper.selectMemberById", 1);
		Member member1 = sqlSession.selectOne("cn.ywang.mybatis.mapper.MemberMapper.selectMemberById", 1);
		System.out.println(member.getName());
		System.out.println(member1.getName());
	}

	@Test
	public void testComplexStatement() {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		SqlSession sqlSession = sqlSessionFactory.openSession();
		TestTaskDto member = sqlSession.selectOne("cn.ywang.mybatis.mapper.TestTaskMapper.selectTaskById", 1);
		System.out.println(member.getTaskName());
		System.out.println(member.getTestCase().getCaseName());
	}

	@Test
	public void testComplexStatement1() {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		SqlSession sqlSession = sqlSessionFactory.openSession();
		TestTaskDto member = sqlSession.selectOne("cn.ywang.mybatis.mapper.TestTaskMapper.selectTask", 1);
		System.out.println(member.getTaskName());
		System.out.println(member.getTestCase().getCaseName());
	}

	@Test
	public void testUpdateStatement2() {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		TestTask testTask = new TestTask();
		testTask.setTaskId(1);
		testTask.setTaskName("修改名称");
		int member = sqlSession.update("cn.ywang.mybatis.mapper.TestTaskMapper.updateTask", testTask);
		System.out.println(member);
	}
}
